import { Button, Space, TypographyText } from 'ant-design-vue'
import { defineComponent, nextTick, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

import { commonMenuApi } from '@/apis'
import { JdCouponItem } from '@/apis/jdCouponTypes'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import { useDialog } from '@/hooks'
import { bus, formatDate } from '@/utils'

export default defineComponent({
  name: 'MinAppIndex',
  setup() {
    const { showConfirm } = useDialog()

    const tableRef = ref({} as ProTableRef)

    const curOptionItem = ref({} as JdCouponItem)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        toolbarConfig: {
          perfect: false,
          slots: {
            buttons: 'toolbarButtons'
          }
        },
        formConfig: {
          items: [
            {
              title: '活动名称',
              field: 'name',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columnConfig: {
          minWidth: 150,
          resizable: true
        },
        columns: [
          {
            title: '活动名称',
            field: 'name'
          },
          {
            title: '副标题',
            field: 'subTitle'
          },
          {
            title: '创建时间',
            field: 'createTime',
            formatter: [TABLE_FORMAT.Date, 'yyyy-MM-dd HH:mm:ss']
          },
          {
            title: '有效期',
            slots: {
              default: 'timeRangeRow'
            }
          },
          {
            title: '状态',
            field: 'status',
            fixed: 'right',
            width: 100,
            slots: {
              default: 'status'
            }
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            },
            width: 150
          }
        ],

        proxyConfig: {
          form: true,
          props: {
            result: 'data'
          },
          ajax: {
            query: async ({ form }) => {
              const data = await commonMenuApi.getIndexBlockList({
                ...form
              })
              return data
            }
          }
        }
      }
    } as ProTableProps)

    //禁用/启用
    function handleDisable(data) {
      showConfirm({
        content: data.status == 1 ? '确认禁用该活动？' : '确认启用该活动？',
        onOk: async () => {
          try {
            await commonMenuApi.upDownBlock(data.id)
            tableRef.value.refresh()
          } catch (error) {}
        }
      })
    }

    onMounted(() => {
      bus.on('refreshIndexBlockList', () => {
        nextTick(() => {
          tableRef.value.refresh()
        })
      })
    })

    const router = useRouter()

    //新增或编辑
    function createOrEdit(item) {
      router.push({
        name: 'AddOrEditActivity',
        query: {
          id: item ? item.id : null
        }
      })
    }

    return {
      tableRef,
      tableConfig,
      curOptionItem,
      createOrEdit,
      handleDisable
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            timeRangeRow: ({ row }) => (
              <Space>
                <span>
                  {row.startTime
                    ? `${formatDate(row.startTime)} 至 ${formatDate(
                        row.endTime
                      )}`
                    : '无'}
                </span>
              </Space>
            ),
            status: ({ row }) => {
              return (
                <Space>
                  {row.status == 0 ? (
                    <TypographyText type="danger">禁用</TypographyText>
                  ) : (
                    <TypographyText type="success">启用</TypographyText>
                  )}
                </Space>
              )
            },
            operation: ({ row }) => (
              <Space>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleDisable(row)}
                >
                  {row.status == 1 ? '禁用' : '启用'}
                </Button>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.createOrEdit(row)}
                >
                  编辑
                </Button>
              </Space>
            ),
            toolbarButtons: () => {
              return (
                <Space>
                  <Button
                    type="primary"
                    onClick={() => {
                      this.createOrEdit(null)
                    }}
                  >
                    新增活动
                  </Button>
                </Space>
              )
            }
          }}
          {...this.tableConfig}
        />
      </div>
    )
  }
})
